package mo.tags.greedy;

public class L861 {

    public int matrixScore(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int ret = m * (1 << (n-1));

        for (int j = 1; j < n; j++) {
            int ones = 0;
            for (int i = 0; i < m; i++) {
                if (grid[i][0] == 1) {
                    ones += grid[i][j];
                } else {
                    ones += 1 - grid[i][j];
                }
            }
            ones = Math.max(ones, m - ones);
            ret += ones * (1 << (n-j-1));
        }
        return ret;
    }

}
